/**
 *JSP项目组版权所有 
 */
package cn.com.change.webapp.action;

import java.util.List;

import cn.com.change.model.School;
import cn.com.change.service.BasedataManager;

import com.opensymphony.xwork2.Preparable;

/**
 * @author cancer
 *
 */
public class BasedataAction extends BaseAction implements  Preparable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;
	private School school;
	private List<School> schools ;
	private BasedataManager bdManager;

	public Long getId() {
		return id;
	}

	public School getSchool() {
		return school;
	}

	public List<School> getSchools() {
		return schools;
	}

	public BasedataManager getBdManager() {
		return bdManager;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setSchool(School school) {
		this.school = school;
	}

	public void setSchools(List<School> schools) {
		this.schools = schools;
	}

	public void setBdManager(BasedataManager bdManager) {
		this.bdManager = bdManager;
	}

	public void prepareSave() throws Exception {
		if (getRequest().getMethod().equalsIgnoreCase("post")){
			if("".equals(getRequest().getParameter("school.id"))){
				this.school = new School();
			}else{
				this.school = bdManager.get(Long.parseLong(
									getRequest().getParameter("school.id")));
			}
		}
	}
	
	public String getAllSchools(){
		this.schools = bdManager.getSchools();
		return SUCCESS;
	}
	
	public String delete(){
		if(id!=null){
			bdManager.remove(id);
		}else{
			bdManager.remove(school.getId());
		}
		   saveMessage(getText("school.deleted"));
		   return SUCCESS;
	}
	
	public String edit(){
		if(null!=id){
			this.school = bdManager.get(id);
		}else{
			this.school = new School();
		}
		
		return SUCCESS;
	}
	
	public String save(){
		if(cancel!=null){
			return CANCEL;
		}
		if(null!=delete){
			return delete();
		}
		
		boolean isNew = (null==school.getId());
		school = bdManager.save(school);
		String key = isNew ? "school.added" : "school.updated";
		saveMessage(getText(key));
		if(isNew){
			return SUCCESS;
		}else{
			return INPUT;
		}
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		
	}		
	
}
